#include<stdio.h>
#include<windows.h>
#include<math.h> 
#define A4_PITCH 440
#define MUSIC_RATIO 1.05946309411451f
typedef enum
{
	C, Cs, D, Ds, E, F, Fs, G, Gs, A, As, B, X
} pitch;
//计算音高的
//参数为：CDEFGAB就是黑白键，数字是第几大调，默认5大调 
DWORD getFreqHz(pitch name, char num)
{
	if (name == X)
		return 0;
	int feq = pow(MUSIC_RATIO, ((int)name + 3)) * A4_PITCH;
	return pow(2, (num - 5))*feq;
}
//把简谱转换为C-A的
pitch getNumPitch(int num){
	pitch p;
	switch(num){
		case 1:{
			p=C;
			break;
		}
		case 2:{
			p=D;
			break;
		}
		case 3:{
			p=E;
			break;
		}
		case 4:{
			p=F;
			break;
		}
		case 5:{
			p=G;
			break;
		}
		case 6:{
			p=A; 
			break;
		}
		case 7:{
			p=B;
			break;
		}
		case 0:{
			p=X;
			break;
		}
	}
	return p;
}
//让电脑发出声音
//参数为：赫兹和时间 
void Voice(DWORD hz,DWORD time){
	Beep(hz,time);
}
//计算节拍的
//参数为：四分音符为1 八分音符为0.5 以此类推，每分钟多少拍 
DWORD getFreqTime(double pit,int hz){
	double onetime=60*1000/4/hz;
	return onetime*pit;
}
//更改光标位置和颜色 
//参数为：x，y，颜色
//颜色示例： 
//FOREGROUND_RED|FOREGROUND_GREEN|FOREGROUND_BLUE|FOREGROUND_INTENSITY
//BACKGROUND_RED|BACKGROUND_GREEN|BACKGROUND_BLUE|BACKGROUND_INTENSITY
void PointXY(int x, int y,WORD color){
	COORD pos;
	HANDLE hOutput;
	pos.X = x;
	pos.Y = y;
	hOutput = GetStdHandle(STD_OUTPUT_HANDLE);
	SetConsoleTextAttribute(hOutput,color);
	SetConsoleCursorPosition(hOutput, pos);
}
int main(){
	char c;
	//默认起调是6，即Do=C
	int num=6;
	while(1){
		//这行和下面那行是接收控制台输入的
		if(_kbhit()){
			c=_getch();
			switch(c){
				//加减用于调节音高
				case '+':{
					num++;
					break;
				}
				case '-':{
					num--;
					break;
				}
				case '1':
				case '2':
				case '3':
				case '4':
				case '5':
				case '6':
				case '7':
				case '0':
					Voice(getFreqHz(getNumPitch(c-'0'),num),getFreqTime(1,60));
			}
		}
	} 
	return 0;
} 

